// 手机号或身份证号脱敏
function FunDesensitization(e, length) {
    if (!!e) {
        if (!length) length = 6;
        let $ = ''
        for (let i = 0; i < length; i++) { $ += '*' }
        const text = e.replace(/\s*/g, "").replace(/(.{3}).*(.{4})/i, '$1' + $ + '$2')
        return text
    }
}

// 日期转年龄
function jsGetAge(strBirthday) {
    if (!strBirthday) return false;
    let returnAge;
    const strBirthdayArr = strBirthday.split("-");
    const birthYear = strBirthdayArr[0];
    const birthMonth = strBirthdayArr[1];
    const birthDay = strBirthdayArr[2];
    const d = new Date();
    const nowYear = d.getFullYear();
    const nowMonth = d.getMonth() + 1;
    const nowDay = d.getDate();
    if (nowYear == birthYear) {
        returnAge = 0;//同年 则为0岁
    } else {
        var ageDiff = nowYear - birthYear; //年之差
        if (ageDiff > 0) {
            if (nowMonth == birthMonth) {
                var dayDiff = nowDay - birthDay;//日之差
                if (dayDiff < 0) {
                    returnAge = ageDiff - 1;
                } else {
                    returnAge = ageDiff;
                }
            } else {
                var monthDiff = nowMonth - birthMonth;//月之差
                if (monthDiff < 0) {
                    returnAge = ageDiff - 1;
                } else {
                    returnAge = ageDiff;
                }
            }
        } else {
            returnAge = -1;//返回-1 表示出生日期输入错误 晚于今天
        }
    }
    return returnAge;//返回周岁年龄
}

// 生成一段验证码
function funCode() {
    const e = 5 // 如果想显示6位数，5改成6即可
    const sCode = "a,b,c,d,e,f,g,h,i,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z,A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0";
    const aCode = sCode.split(",");
    const aLength = aCode.length;
    let text = ''
    for (let i = 0; i < e; i++) {
        const j = Math.floor(Math.random() * aLength);//获取到随机的索引值
        const txt = aCode[j];
        text += txt
    }
    // this.$set(this, 'thisCode', text)
    return text // 生成的值
}

export default {
    FunDesensitization,
    jsGetAge,
    funCode
}